package com.luoran.bartalks.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.annotation.OperationLog;
import com.luoran.bartalks.entity.biz.UserInfo;
import com.luoran.bartalks.service.IUserService;
import com.luoran.bartalks.utils.EnumOperationType;
import com.luoran.bartalks.utils.MD5;

/**
 * @author lifetime
 *
 */
@RestController
@RequestMapping("/api/login")
public class AppLoginApi {

	@Autowired
	private IUserService userService;


	@RequestMapping("auth")
	@OperationLog(EnumOperationType.LOGIN)
	public String auth(String number, String pwd) {
		JSONObject json = new JSONObject();
		UserInfo info = userService.getUser(number, MD5.get(pwd));
		if (info == null) {
			json.put("state", false);
			json.put("msg", "账号或密码错误！");
		} else {
			String token = AppUser.put(info);
			json.put("state", true);
			json.put("token", token);
			json.put("userInfo", info);
		}
		return json.toJSONString();
	}

	@RequestMapping("out")
	@OperationLog(EnumOperationType.LOGOUT)
	public String out(String number) {
		JSONObject json = new JSONObject();
		AppUser.clear(number);
		json.put("state", true);
		return json.toJSONString();
	}
}
